package org.report4service.utils;

import org.report4service.model.User;

/**
 * Used to extract the user object from a string saved in the db and construct
 * up all the elements of the user object into a single string to be saved into
 * the database.
 * 
 * @author Jmpango
 * 
 */
public class UserParser {

    private String _newRecordString, _username, _password;
    private User user = null;
    private int _index = 0;

    /** new instance of the userParser */
    public UserParser() {
    }

    /**
     * The record format: usernam|password
     * 
     * @param record
     * @return
     */
    public User getUser(String record) {
	extract(record);
	return user;
    }

     public User getUser(String record, int recordId, int maxRecords) {
       if(recordId > maxRecords){
           return new User();
       }
       extract(record);
       user = new User(_username, _password);
       return user;
     }

     private void extract(String record) {
        _newRecordString = record;
        _index = _newRecordString.indexOf("|");
        _username = _newRecordString.substring(0, _index);
        _password = _newRecordString.substring(_index + 1, _newRecordString.length());
        user = new User(_username, _password);
    }

    /**
     * Returns a string containing user details that is to be saved in the db.
     * 
     * @param user
     * @return
     */
    public String getUserRecord(User user) {
	return user.getUsername() + "|" + user.getPassword();
    }

    /**
     * @return the _newRecordString
     */
    public String get_newRecordString() {
	return _newRecordString;
    }

    /**
     * @param newRecordString
     *            the _newRecordString to set
     */
    public void set_newRecordString(String newRecordString) {
	_newRecordString = newRecordString;
    }

    /**
     * @return the _username
     */
    public String get_username() {
	return _username;
    }

    /**
     * @param username
     *            the _username to set
     */
    public void set_username(String username) {
	_username = username;
    }

    /**
     * @return the _password
     */
    public String get_password() {
	return _password;
    }

    /**
     * @param password
     *            the _password to set
     */
    public void set_password(String password) {
	_password = password;
    }

    /**
     * @return the user
     */
    public User getUser() {
	return user;
    }

    /**
     * @param user
     *            the user to set
     */
    public void setUser(User user) {
	this.user = user;
    }

    /**
     * @return the _index
     */
    public int get_index() {
	return _index;
    }

    /**
     * @param index
     *            the _index to set
     */
    public void set_index(int index) {
	_index = index;
    }
}
